package cn.exrick.xboot.modules.CheckConn;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.CheckConn.entity.CheckConn;
import cn.exrick.xboot.modules.CheckConn.service.impl.CheckConnImpl;
import cn.exrick.xboot.modules.commonLink.entity.CommonLink;
import cn.exrick.xboot.modules.system.entity.System;
import cn.exrick.xboot.modules.system.service.ISystemService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: njp
 * @Date: 2020/05/27/17:46
 * @Description:
 */

@Slf4j
@RestController
@RequestMapping("/xboot/CheckConn")
public class CheckConnController {
    @Autowired
    private CheckConnImpl checkConnimpl;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ISystemService iSystemService;
    private static CheckConn checkConn;

    public String getRdisvalueByKey(CheckConn checkConn){
        checkConn=checkConn;
        String value=null;
        value = redisTemplate.opsForValue().get(CommonConstant.checkConn + checkConn.getSender());
        if(!StrUtil.isBlank(value)){
            //如果redis中没有对应的值说明连接异常
            updateCheckConnStatus();
        }
        return "";

    }

    public  void updateCheckConnStatus(){
        System system = new System();
        //将状态设置为异常
        system.setConnStatus("2");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("module_name",checkConn.getSender());
        boolean update = iSystemService.update(system, queryWrapper);
        log.info(update?"修改服务状态成功":"修改服务状态失败");

    }


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ApiOperation(value = "test")
    public void test(){
        checkConn=new CheckConn(6,"","FD");
        checkConn=checkConn;
        String value=null;
        value = redisTemplate.opsForValue().get(CommonConstant.checkConn + checkConn.getSender());
        if(StrUtil.isBlank(value)){
            //如果redis中没有对应的值说明连接异常
            checkConnimpl.updateCheckConnStatus();
        }

        //return new ResultUtil<List<CommonLink>>().setData(list);
    }




}
